package com.ruiysoft.crm.task;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruiysoft.crm.model.entity.BizClient;
import com.ruiysoft.crm.service.BizClientService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.LocalDate;
import java.util.List;

/**
 * @author fanyl
 * @version 1.0
 * @className CrmScheduleTask
 * @description [凌晨一点执行定时任务：查询保险到期时间少于3月的客户数据重置状态]
 * @date 2019/12/1 9:00 下午
 */
@Slf4j
@Service
public class CrmScheduleTask {

    @Autowired
    BizClientService bizClientService;

    /**
     * 查询保险到期时间少于3个月的客户数据，并更新保险状态为null
     */
    @Scheduled(cron = "0 35 22 * * ?")
    public void insuranceExpireClientStatus() {

        LocalDate expireDate = LocalDate.now().plusDays(90);
        QueryWrapper<BizClient> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("CLIVTAEndDate", expireDate.toString()).or().eq("VIEndDate", expireDate.toString());

        List<BizClient> bizClientList = bizClientService.list(queryWrapper);
        if (!CollectionUtils.isEmpty(bizClientList)) {
            bizClientList.forEach(bizClient -> bizClient.setSuccess(null));
            bizClientService.updateBatchById(bizClientList);
        }
    }
}
